package com.lizy.service.impl;

import com.lizy.mapper.RolePermissionMapper;
import com.lizy.mapper.RolesMapper;
import com.lizy.mapper.UsersMapper;
import com.lizy.pojo.Permissions;
import com.lizy.pojo.RolePermission;
import com.lizy.pojo.Roles;
import com.lizy.service.RolesService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 19688
 * @description 针对表【roles】的数据库操作Service实现
 * @createDate 2024-09-02 22:33:19
 */
@Service
public class RolesServiceImpl extends ServiceImpl<RolesMapper, Roles>
        implements RolesService {

    @Autowired
    RolesMapper rolesMapper;
    @Autowired
    RolePermissionMapper rolePermissionMapper;

    @Autowired
    UsersMapper usersMapper;

    @Override
    public void insertRoles(Roles roles) {


        rolesMapper.insert(roles);

        List<Permissions> permissionsList = roles.getPermissionsList();

        /*for (Permissions permissions : permissionsList) {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(roles.getRoleId());
            rolePermission.setPermissionId(permissions.getPermissionId());
            rolePermissionMapper.insert(rolePermission);
        }*/

        permissionsList.stream()
                .forEach(permissions -> {
                    RolePermission rolePermission = new RolePermission();
                    rolePermission.setRoleId(roles.getRoleId());
                    rolePermission.setPermissionId(permissions.getPermissionId());
                    rolePermissionMapper.insert(rolePermission);
                 });
    }

    @Override
    public void deleteRoles(Integer roleId) {
        QueryWrapper<RolePermission> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(RolePermission::getRoleId, roleId);
        rolePermissionMapper.delete(wrapper);
        rolesMapper.deleteById(roleId);
    }

    @Override
    public void updateByRoles(Roles roles) {

        QueryWrapper<RolePermission> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(RolePermission::getRoleId, roles.getRoleId());
        rolePermissionMapper.delete(wrapper);

        List<Permissions> permissionsList = roles.getPermissionsList();

     /*   for (Permissions permissions : permissionsList) {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(roles.getRoleId());
            rolePermission.setPermissionId(permissions.getPermissionId());
            rolePermissionMapper.insert(rolePermission);
        }*/


        permissionsList.stream()
                .forEach(permissions -> {
                    RolePermission rolePermission = new RolePermission();
                    rolePermission.setRoleId(roles.getRoleId());
                    rolePermission.setPermissionId(permissions.getPermissionId());
                    rolePermissionMapper.insert(rolePermission);
                });


        rolesMapper.updateById(roles);


    }


}




